package leetcode.editor.cn.q1_300.q;

/**
 * @author qidi
 * @date 2021-11-18 15:33:11
 */
class Q0038_CountAndSay {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String countAndSay(int n) {
            if (n == 1) return "1";
            if (n == 2) return "11";
            StringBuilder s = new StringBuilder("11");

            for (int i = 1; i < n - 1; i++) {
                int j = 1;
                String ss = s.toString();
                s = new StringBuilder();
                int num = 1;
                char pre = ss.charAt(0);
                while (j < ss.length()) {
                    if (pre != ss.charAt(j)) {
                        s.append(num);
                        s.append(pre);
                        num = 1;
                        pre = ss.charAt(j);
                    } else {
                        num++;
                    }

                    j++;
                }
                s.append(num);
                s.append(pre);
            }

            return s.toString();
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0038_CountAndSay().new Solution();
        for (int i = 0; i < 5; i++) {
            System.out.println(solution.countAndSay(i + 1));
        }
    }
}